package com.aracky.system.controller;

import com.aracky.common.util.ConvertUtil;
import com.aracky.common.vo.CommonResult;
import com.aracky.common.vo.PageVO;
import com.aracky.system.model.entity.SysPost;
import com.aracky.system.model.form.SysPostForm;
import com.aracky.system.model.form.SysUserPostForm;
import com.aracky.system.model.request.PostDeptRequest;
import com.aracky.system.model.request.SysPostRequest;
import com.aracky.system.model.request.SysUserRequest;
import com.aracky.system.model.vo.PostDeptVO;
import com.aracky.system.model.vo.SysPostVO;
import com.aracky.system.model.vo.SysUserVO;
import com.aracky.system.service.SysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2022-07-14 11:22:57
 */
@RestController
@RequestMapping("/sysPost")
@Api(tags = "岗位相关接口")
public class SysPostController {
    @Resource
    private SysPostService sysPostService;

    /**
     * 分页查询岗位部门列表信息
     * @param postDeptRequest
     * @return
     */
    @PostMapping("/getPostDept")
    @ApiOperation(value = "分页查询岗位部门信息列表")
    public CommonResult<PageVO<PostDeptVO>> getPostDept(@RequestBody PostDeptRequest postDeptRequest){
        PageVO<PostDeptVO> pageVO = sysPostService.getPostDept(postDeptRequest);
        if (null == pageVO) {
            return CommonResult.error("查询失败！");
        }
        return CommonResult.success(pageVO,"查询成功！");
    }

    @ApiOperation(value = "岗位分页查询")
    @PostMapping(value = "/pageList")
    public CommonResult<PageVO<SysPostVO>> pageList(@RequestBody SysPostRequest sysPostRequest){
        PageVO<SysPostVO> pageVO=sysPostService.getPostPage(sysPostRequest);
        return CommonResult.success(pageVO);
    }
    @ApiOperation(value = "岗位新增")
    @PostMapping("/save")
    public CommonResult savePost(@RequestBody SysPostForm sysPostForm){
        int result=sysPostService.savePost(ConvertUtil.sourceToTarget(sysPostForm, SysPost.class));
        if(result>0){
            return CommonResult.success("新增岗位成功!");
        }
        return CommonResult.error("新增岗位失败!");
    }
    @ApiOperation(value = "修改岗位")
    @PutMapping("/update")
    public CommonResult updatePost(@RequestBody SysPostForm sysPostForm){
        int result=sysPostService.updatePost(ConvertUtil.sourceToTarget(sysPostForm,SysPost.class));
        if(result>0){
            return CommonResult.success("修改岗位成功!");
        }
        return CommonResult.error("修改岗位失败!");
    }
    @ApiOperation(value = "删除岗位")
    @DeleteMapping("/{postId}")
    public CommonResult deletePost(@PathVariable Long postId){
        int result=sysPostService.deletePost(postId);
        if(result>0){
            return CommonResult.success("删除岗位成功!");
        }
        return CommonResult.error("删除岗位失败!");
    }
    @ApiOperation(value = "查询岗位详情")
    @GetMapping("/{postId}")
    public CommonResult<SysPostVO> getPostInfo(@PathVariable Long postId){
        SysPostVO sysPostVO=ConvertUtil.sourceToTarget(sysPostService.getPostInfo(postId),SysPostVO.class);
        if(!Objects.isNull(sysPostVO)){
            return CommonResult.success(sysPostVO,"查询详情成功!");
        }
        return CommonResult.error("查询详情失败!");
    }
    @ApiOperation(value = "岗位绑定用户")
    @PostMapping("/bind/user")
    public CommonResult saveUserPost(@RequestBody SysUserPostForm sysUserPostForm){
        sysPostService.saveUserPost(sysUserPostForm.getPostId(),sysUserPostForm.getUserIds());
        return CommonResult.success("操作成功!");
    }
}
